<?php

/**
 * personne actions.
 *
 * @package    TC5
 * @subpackage personne
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class personneActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->personne_list = PersonnePeer::doSelect(new Criteria());
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->personne = PersonnePeer::retrieveByPk($request->getParameter('id'));
    $this->forward404Unless($this->personne);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new PersonneForm();
  }
  protected function executeAddToPanier(sfWebRequest $request)
  {
  	if ($request->hasparameter('id')){
  		$produit = ProduitPeer::retrieveByPk($request->getParameter('id'));
  		$produit->addToPanier();
  	}
  	$this->redirect('produit/show?id='.$request->getParameter('id'));
  }
  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post'));

    $this->form = new PersonneForm();

   
    
    $user = new sfGuardUser();
    $user->setUsername($this->getRequestParameter('pseudo'));
    $user->setPassword($this->getRequestParameter('password'));
    $user->setIsActive(true);
	$user->save();
    $user = sfGuardUserPeer::retrieveByUsername($this->getRequestParameter('pseudo'), false);
    
    $key = md5($user->getSalt().$user->getUsername());
    $request->setParameter('password', $key);
    $request->setParameter('id', $user->getId());


    $this->processForm($request, $this->form);
    
     
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($personne = PersonnePeer::retrieveByPk($request->getParameter('id')), sprintf('Object personne does not exist (%s).', $request->getParameter('id')));
    $this->form = new PersonneForm($personne);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
    $this->forward404Unless($personne = PersonnePeer::retrieveByPk($request->getParameter('id')), sprintf('Object personne does not exist (%s).', $request->getParameter('id')));
    $this->form = new PersonneForm($personne);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($personne = PersonnePeer::retrieveByPk($request->getParameter('id')), sprintf('Object personne does not exist (%s).', $request->getParameter('id')));
    $personne->delete();

    $this->redirect('personne/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $personne = $form->save();

      $this->redirect('personne/edit?id='.$personne->getId());
    }
  }
}
